﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VPC.BO;
using VPC.BL;
using VPC.EX;

namespace VPC.UI
{
    public partial class ViewAllContacts : Form
    {
        public ViewAllContacts()
        {
            InitializeComponent();
        }

        public void ViewAllContacts_Load()
        {
            List<ContactBO> lstContactDetails = new List<ContactBO>();

            lstContactDetails = ContactBL.GetAllContactDetails();

            dataGridViewContactDetails.DataSource = lstContactDetails;
        }

        private void dataGridViewContactDetails_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            labelContactIdResult.Text = dataGridViewContactDetails.SelectedCells[0].Value.ToString();

            string sFName = dataGridViewContactDetails.SelectedCells[1].Value.ToString();
            string sLName = dataGridViewContactDetails.SelectedCells[2].Value.ToString();

            sFName = sFName.Trim();
            sLName = sLName.Trim();
            labelNameResult.Text = sFName + " " + sLName;

            labelFonctionResult.Text        = dataGridViewContactDetails.SelectedCells[3].Value.ToString();
            labelCompagnyResult.Text        = dataGridViewContactDetails.SelectedCells[4].Value.ToString();
            labelMobileNumber1Result.Text   = dataGridViewContactDetails.SelectedCells[5].Value.ToString();
            labelMobileNumber2Result.Text   = dataGridViewContactDetails.SelectedCells[6].Value.ToString();
            labelMobileNumber3Result.Text   = dataGridViewContactDetails.SelectedCells[7].Value.ToString();
            labelHomeNumber1Result.Text     = dataGridViewContactDetails.SelectedCells[8].Value.ToString();
            labelHomeNumber2Result.Text     = dataGridViewContactDetails.SelectedCells[9].Value.ToString();
            labelHomeNumber3Result.Text     = dataGridViewContactDetails.SelectedCells[10].Value.ToString();
            labelWorkNumber1Result.Text     = dataGridViewContactDetails.SelectedCells[11].Value.ToString();
            labelWorkNumber2Result.Text     = dataGridViewContactDetails.SelectedCells[12].Value.ToString();
            labelWorkNumber3Result.Text     = dataGridViewContactDetails.SelectedCells[13].Value.ToString();
            labelFaxNumber1Result.Text      = dataGridViewContactDetails.SelectedCells[14].Value.ToString();
            labelFaxNumber2Result.Text      = dataGridViewContactDetails.SelectedCells[15].Value.ToString();
            labelFaxNumber3Result.Text      = dataGridViewContactDetails.SelectedCells[16].Value.ToString();
            labelStreetResult.Text          = dataGridViewContactDetails.SelectedCells[17].Value.ToString();
            labelLocalityResult.Text        = dataGridViewContactDetails.SelectedCells[18].Value.ToString();
            labelCodeResult.Text            = dataGridViewContactDetails.SelectedCells[19].Value.ToString();
            labelCountryResult.Text         = dataGridViewContactDetails.SelectedCells[20].Value.ToString();
            labelEmail1Result.Text          = dataGridViewContactDetails.SelectedCells[21].Value.ToString();
            labelEmail2Result.Text          = dataGridViewContactDetails.SelectedCells[22].Value.ToString();
            labelEmail3Result.Text          = dataGridViewContactDetails.SelectedCells[23].Value.ToString();


            buttonEdit.Visible = true;
            buttonDelete.Visible = true;

        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            Int32 iContactId = Convert.ToInt32(labelContactIdResult.Text);

            DialogResult result1 = MessageBox.Show("Are you sure you want to delete this Contact?",
                                  "Delete the Contact",
                                   MessageBoxButtons.YesNo);

            if (result1 == DialogResult.Yes)
            {
                int result = ContactBL.RemoveContact(iContactId);

                if (result > 0)
                {
                    MessageBox.Show("Contact Removed Successfully");
                    ViewAllContacts_Load();
                    set_undefined();
                }
            }
        }
        
        private void set_undefined()
        {
            this.labelFonctionResult.Text = "Undefined";
            this.labelCompagnyResult.Text = "Undefined";
            this.labelNameResult.Text = "Undefined";
            this.labelContactIdResult.Text = "Undefined";
            this.labelFaxNumber3Result.Text = "Undefined";
            this.labelFaxNumber2Result.Text = "Undefined";
            this.labelFaxNumber1Result.Text = "Undefined";
            this.labelWorkNumber3Result.Text = "Undefined";
            this.labelWorkNumber2Result.Text = "Undefined";
            this.labelWorkNumber1Result.Text = "Undefined";
            this.labelHomeNumber3Result.Text = "Undefined";
            this.labelHomeNumber2Result.Text = "Undefined";
            this.labelHomeNumber1Result.Text = "Undefined";
            this.labelMobileNumber3Result.Text = "Undefined";
            this.labelMobileNumber2Result.Text = "Undefined";
            this.labelMobileNumber1Result.Text = "Undefined";
            this.labelEmail3Result.Text = "Undefined";
            this.labelEmail2Result.Text = "Undefined";
            this.labelEmail1Result.Text = "Undefined";
            this.labelCodeResult.Text = "Undefined";
            this.labelLocalityResult.Text = "Undefined";
            this.labelStreetResult.Text = "Undefined";
            this.labelCountryResult.Text = "Undefined";
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            EditContact frmEditContact = new EditContact();
            frmEditContact.labelContactId.Text = labelContactIdResult.Text;
            frmEditContact.EditContact_Load();
            frmEditContact.ShowDialog();
            this.ViewAllContacts_Load();
            this.set_undefined();
        }

        private void buttonExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
            saveFileDialog1.Filter = "vCard (*.vcf)|*.vcf";
            saveFileDialog1.FilterIndex = 1;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Console.WriteLine(saveFileDialog1.FileName);//Do what you want here

                ContactEX jo = new ContactEX(saveFileDialog1.FileName);

                Int32 iContactId = Convert.ToInt32(labelContactIdResult.Text);
                ContactBO objContactBO = new ContactBO();
                objContactBO = ContactBL.GetContactDetails(iContactId);

                jo._surname = objContactBO.FirstName;
                jo._given = objContactBO.LastName;
                jo._title = labelFonctionResult.Text;
                jo._org = labelCompagnyResult.Text;

                if (labelEmail1Result.Text != "Undefined")
                    jo.addEmail(labelEmail1Result.Text);
                if (labelEmail2Result.Text != "Undefined")
                    jo.addEmail(labelEmail2Result.Text);
                if (labelEmail3Result.Text != "Undefined")
                    jo.addEmail(labelEmail3Result.Text);

                if (labelMobileNumber1Result.Text != "Undefined")
                    jo.addPhone(etype.cell, labelMobileNumber1Result.Text);
                if (labelMobileNumber2Result.Text != "Undefined")
                    jo.addPhone(etype.cell, labelMobileNumber2Result.Text);
                if (labelMobileNumber3Result.Text != "Undefined")
                    jo.addPhone(etype.cell, labelMobileNumber3Result.Text);

                if (labelHomeNumber1Result.Text != "Undefined")
                    jo.addPhone(etype.home, labelHomeNumber1Result.Text);
                if (labelHomeNumber2Result.Text != "Undefined")
                    jo.addPhone(etype.home, labelHomeNumber2Result.Text);
                if (labelHomeNumber3Result.Text != "Undefined")
                    jo.addPhone(etype.home, labelHomeNumber3Result.Text);

                if (labelWorkNumber1Result.Text != "Undefined")
                    jo.addPhone(etype.work, labelWorkNumber1Result.Text);
                if (labelWorkNumber2Result.Text != "Undefined")
                    jo.addPhone(etype.work, labelWorkNumber2Result.Text);
                if (labelWorkNumber3Result.Text != "Undefined")
                    jo.addPhone(etype.work, labelWorkNumber3Result.Text);

                if (labelFaxNumber1Result.Text != "Undefined")
                    jo.addPhone(etype.fax, labelFaxNumber1Result.Text);
                if (labelFaxNumber2Result.Text != "Undefined")
                    jo.addPhone(etype.fax, labelFaxNumber2Result.Text);
                if (labelFaxNumber3Result.Text != "Undefined")
                    jo.addPhone(etype.fax, labelFaxNumber3Result.Text);

                jo.addAddress(etype.work, labelStreetResult.Text, labelLocalityResult.Text, labelCodeResult.Text, labelCountryResult.Text);
                jo.export();

            } 
        }
    }
}
